package com.dhcc.DRGsHosp.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.DRGsHosp.facade.DepGroupFacade;
import com.dhcc.DRGsHosp.facade.dto.DepGroupDTO;

@Controller("drgsHospWebController8")
@RequestMapping("/DepGroup")
public class DepGroupController {
		
	@Autowired
	private DepGroupFacade depGroupFacade;
	
	@ResponseBody
	@RequestMapping("/add")
	public InvokeResult add(DepGroupDTO depGroupDTO) {
		return depGroupFacade.creatDepGroup(depGroupDTO);
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public InvokeResult update(DepGroupDTO depGroupDTO) {
		return depGroupFacade.updateDepGroup(depGroupDTO);
	}
	
	@ResponseBody
	@RequestMapping("/pageJson")
	public Page pageJson(DepGroupDTO depGroupDTO, @RequestParam int page, @RequestParam int pagesize) {
		Page<DepGroupDTO> all = depGroupFacade.pageQueryDepGroup(depGroupDTO, page, pagesize);
		return all;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public InvokeResult remove(@RequestParam String depGroupCodes) {
		String[] value = depGroupCodes.split(",");
        String[] idArrs = new String[value.length];
        for (int i = 0; i < value.length; i ++) {
        					idArrs[i] = value[i];
			        				        }
        return depGroupFacade.removeDepGroups(idArrs);
	}
	
	@ResponseBody
	@RequestMapping("/get/{depGroupCode}")
	public InvokeResult get(@PathVariable String depGroupCode) {
		return depGroupFacade.getDepGroup(depGroupCode);
	}
	
		
    @InitBinder    
    public void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    
        dateFormat.setLenient(false);    
        //CustomDateEditor 可以换成自己定义的编辑器。
        //注册一个Date 类型的绑定器 。
        binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
    
    @ResponseBody
	@RequestMapping("/findAllDepGroup")
    public InvokeResult findAllDepGroup(){
    	return InvokeResult.success(depGroupFacade.findAllDepGroup());
    }
    
    /**
     * 
     * @methodname    :isOrderExist
     * @description   : 查询该order序号是否已存在
     * @param         :
     * @author        :fulianxia
     * @return        :
     * @date          :2015年7月1日
     */
    @ResponseBody
   	@RequestMapping("/isOrderExist")
       public Boolean isOrderExist(String order,String code){
       	return depGroupFacade.isOrderExist(order,code);
       }
	
}
